trigger SyncSaloonToCallPlan on HCPEducation__c (after update,after delete) {
    
    if(system.trigger.isupdate){
        list<id> HCPEducationId=new list<id>();
        set<Date> SortDate=new set<date>();
        set<id> ownerId=new set<id>();
        Map<id,date> HCPEducationDateMap=new map<id,date>();
        for(hcpeducation__c hcpedu:trigger.new){
            if(trigger.oldmap.get(hcpedu.id).date__c != hcpedu.date__c && hcpedu.HCPeducationactivitytype__c!='科内会'){
                hcpeducationid.add(hcpedu.id);
                HCPEducationDateMap.put(hcpedu.id,hcpedu.date__c);
                sortdate.add(hcpedu.date__c);
                sortdate.add(trigger.oldmap.get(hcpedu.id).date__c);
            }
        }
        
        
        
        list<callplan__c> CallPlanList=new list<callplan__c>();
        if(hcpeducationid.size()>0){
            callplanlist=[select id,ActivityId__c from callplan__c where ActivityId__c in: HCPEducationId];
        }
        for(CallPlan__c cp:callplanlist){
            cp.CallPlanDate__c=HCPEducationDateMap.get(cp.activityid__c);
            cp.calldate__c=datetime.newinstance(HCPEducationDateMap.get(cp.activityid__c),time.newInstance(9,0,0,0));
        }
        update callplanlist;
        
        list<event> eventlist=new list<event>();
        if(hcpeducationid.size()>0){
            eventlist=[select id,whatid,ownerid from event where whatid in: HCPEducationId];
        }
        for(event e:eventlist){
            e.StartDateTime=datetime.newinstance(HCPEducationDateMap.get(e.whatid),time.newInstance(9,0,0,0));
            e.eventdate__c=HCPEducationDateMap.get(e.whatid);
            ownerId.add(e.ownerid);
        }
        update eventlist;
        if(hcpeducationid.size()>0){
            CalendarSorterNotFuture.sort(sortDate,Ownerid);
        }
    }
    if(system.trigger.isdelete){
        list<id> HCPEducationId=new list<id>();
        set<Date> SortDate=new set<date>();
        set<id> ownerId=new set<id>();
        for(hcpeducation__c hcpedu:trigger.old){
            if(hcpedu.HCPeducationactivitytype__c!='科内会'){
                hcpeducationid.add(hcpedu.id);
                sortdate.add(hcpedu.date__c);
            }
        }
        list<callplan__c> CallPlanList=new list<callplan__c>();
        list<event> eventlist=new list<event>();
        if(HCPeducationid.size()>0){
            callplanlist=[select id from callplan__c where ActivityId__c in: hcpeducationId];
            eventlist=[select id,ownerid from event where whatid in: hcpeducationId];
        }
        delete callplanlist;
        for(event e:eventlist){
            ownerid.add(e.ownerid);
        }
        delete eventlist;
        CalendarSorterNotFuture.sort(sortDate,Ownerid);
    }
}